package dapps.entity.document;


import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.OrderBy;
import org.springside.modules.utils.DateUtils;

import dapps.entity.IdEntity;
import dapps.entity.user.User;


@Entity
// 表名与类名不相同时重新定义表名.
@Table(name = "document")
// 默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Document extends IdEntity implements Serializable  {

	private static final long serialVersionUID = 3039880880168719710L;

	private String name;
	
	private Category category;

	private User postuser;
	
	private String content;
	
	private Set<Attachment> attachments = new HashSet<Attachment>(0);
	
	@Column(insertable = true, updatable = false)
	private Date insertDate = DateUtils.getCurrentDateTime();

	@Column(insertable = false, updatable = true)
	private Date updateDate;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "category_id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Category getCategory() {
		return category;
	}


	public void setCategory(Category category) {
		this.category = category;
	}

	
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "doc")
	@OrderBy(clause="id asc")
    public Set<Attachment> getAttachments() {
            return attachments;
    }


	public void setAttachments(Set<Attachment> attachments) {
		this.attachments = attachments;
	}

	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "postuser_id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public User getPostuser() {
		return postuser;
	}

	public void setPostuser(User postuser) {
		this.postuser = postuser;
	}

	public Date getInsertDate() {
		return insertDate;
	}

	public void setInsertDate(Date insertDate) {
		this.insertDate = insertDate;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}


	
}